package org.windblog.core.servlet.file;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.windblog.core.FileService;
import org.windblog.core.entity.File;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

@SuppressWarnings("serial")
public class DeleteFileServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String key = request.getParameter("filekey");
		File file = FileService.getFile(key);
		if(file==null)
		{
			response.sendRedirect("/admin/mediaLibrary.jsp?message=5");
		}else{
			BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
			bs.delete(file.getBlobKey());
			FileService.deleteFile(file);
			response.sendRedirect("/admin/mediaLibrary.jsp?message=6");
		}
	}
}